using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Rensselaer_Event_Finder
{
    public partial class HomeMDI : Form
    {

        #region Variables

        //instance of each event shown on the home page
        public static Event event1;
        public static Event event2;
        public static Event event3;
        public static Event event4;
        public static Event event5;
        public static Event event6;
        public static Event event7;
        public static Event event8;

        public static Location newvenue1;
        public static Location newvenue2;
        public static Location newvenue3;
        public static Location newvenue4;

        public static Location topvenue1;
        public static Location topvenue2;

        #endregion

        #region Public Methods

        public HomeMDI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Populates all of the controls on the home page with the corresponding
        /// events and venues from the DB.
        /// </summary>
        public void load_home()
        {
            //TODO: get upcoming events, top venues, and new venues
            IList<Event> upcomingEvents = Globals.getEventService().getUpcomingEvents(12);
            
            //populate the controls
            event1name.Text = event1.Name;
            newvenue1name.Text = newvenue1.Name;
            newvenue2name.Text = newvenue2.Name;
            topvenue1name.Text = topvenue1.Name;
            topvenue2name.Text = topvenue2.Name;

            event1date.Text = event1.Description;
            event1pic.Tag = event1.Id;
            event1name.Tag = event1.Id;
        }

        public void populateHomePage()
        {
            event1name.Text = event1.Name;
            event1name.Tag = event1.Id;
            event1date.Text = event1.StartTime.ToShortDateString();
            event1time.Text = event1.StartTime.ToShortTimeString() + "-" + event1.EndTime.ToShortTimeString();
            if (event1.Image_url != "" && event1.Image_url != null)
            {
                event1pic.Image = Globals.loadImageFromURL(event1.Image_url);
            }
            else
            {
                event1pic.Image = Globals.loadImageFromURL(event1.Location.Image_url);
            } 
            event1pic.Tag = event1.Location.Id;
            event1venue.Text = event1.Location.Name;

            event2name.Text = event2.Name;
            event2name.Tag = event2.Id;
            event2date.Text = event2.StartTime.ToShortDateString();
            event2time.Text = event2.StartTime.ToShortTimeString() + "-" + event2.EndTime.ToShortTimeString();
            if (event2.Image_url != "" && event2.Image_url != null)
            {
                event2pic.Image = Globals.loadImageFromURL(event2.Image_url);
            }
            else
            {
                event2pic.Image = Globals.loadImageFromURL(event2.Location.Image_url);
            } 
            event2pic.Tag = event2.Location.Id;
            event2venue.Text = event2.Location.Name;

            event3name.Text = event3.Name;
            event3name.Tag = event3.Id;
            event3date.Text = event3.StartTime.ToShortDateString();
            event3time.Text = event3.StartTime.ToShortTimeString() + "-" + event3.EndTime.ToShortTimeString();
            if (event3.Image_url != "" && event3.Image_url != null)
            {
                event3pic.Image = Globals.loadImageFromURL(event3.Image_url);
            }
            else
            {
                event3pic.Image = Globals.loadImageFromURL(event3.Location.Image_url);
            } 
            event3pic.Tag = event3.Location.Id;
            event3venue.Text = event3.Location.Name;

            event4name.Text = event4.Name;
            event4name.Tag = event4.Id;
            event4date.Text = event4.StartTime.ToShortDateString();
            event4time.Text = event4.StartTime.ToShortTimeString() + "-" + event4.EndTime.ToShortTimeString();
            if (event4.Image_url != "" && event4.Image_url != null)
            {
                event4pic.Image = Globals.loadImageFromURL(event4.Image_url);
            }
            else
            {
                event4pic.Image = Globals.loadImageFromURL(event4.Location.Image_url);
            } 
            event4pic.Tag = event4.Location.Id;
            event4venue.Text = event4.Location.Name;

            event5name.Text = event5.Name;
            event5name.Tag = event5.Id;
            event5date.Text = event5.StartTime.ToShortDateString();
            event5time.Text = event5.StartTime.ToShortTimeString() + "-" + event5.EndTime.ToShortTimeString();
            if (event5.Image_url != "" && event5.Image_url != null)
            {
                event5pic.Image = Globals.loadImageFromURL(event5.Image_url);
            }
            else
            {
                event5pic.Image = Globals.loadImageFromURL(event5.Location.Image_url);
            } 
            event5pic.Tag = event5.Location.Id;
            event5venue.Text = event5.Location.Name;

            event6name.Text = event6.Name;
            event6name.Tag = event6.Id;
            event6date.Text = event6.StartTime.ToShortDateString();
            event6time.Text = event6.StartTime.ToShortTimeString() + "-" + event6.EndTime.ToShortTimeString();
            if (event6.Image_url != "" && event6.Image_url != null)
            {
                event6pic.Image = Globals.loadImageFromURL(event6.Image_url);
            }
            else
            {
                event6pic.Image = Globals.loadImageFromURL(event6.Location.Image_url);
            } 
            event6pic.Tag = event6.Location.Id;
            event6venue.Text = event6.Location.Name;

            event7name.Text = event7.Name;
            event7name.Tag = event7.Id;
            event7date.Text = event7.StartTime.ToShortDateString();
            event7time.Text = event7.StartTime.ToShortTimeString() + "-" + event7.EndTime.ToShortTimeString();
            if (event7.Image_url != "" && event7.Image_url != null)
            {
                event7pic.Image = Globals.loadImageFromURL(event7.Image_url);
            }
            else
            {
                event7pic.Image = Globals.loadImageFromURL(event7.Location.Image_url);
            }
            event7pic.Tag = event7.Location.Id;
            event7venue.Text = event7.Location.Name;

            event8name.Text = event8.Name;
            event8name.Tag = event8.Id;
            event8date.Text = event8.StartTime.ToShortDateString();
            event8time.Text = event8.StartTime.ToShortTimeString() + "-" + event8.EndTime.ToShortTimeString();
            if (event8.Image_url != "" && event7.Image_url != null)
            {
                event8pic.Image = Globals.loadImageFromURL(event8.Image_url);
            }
            else
            {
                event8pic.Image = Globals.loadImageFromURL(event8.Location.Image_url);
            }
            event8pic.Tag = event8.Location.Id;
            event8venue.Text = event8.Location.Name;

            topvenue1name.Text = topvenue1.Name;
            topvenue1name.Tag = topvenue1.Id;
            topvenue1location.Text = topvenue1.City + "," + topvenue1.State;
            topvenue1category.Text = "";
            foreach (Category cat in topvenue1.Categories)
            {
                topvenue1category.Text += cat.Name;
            }
            topvenue1pic.Image = Globals.loadImageFromURL(topvenue1.Image_url);
            topvenue1pic.Tag = topvenue1.Id;

            topvenue2name.Text = topvenue2.Name;
            topvenue2name.Tag = topvenue2.Id;
            topvenue2location.Text = topvenue2.City + "," + topvenue2.State;
            topvenue2category.Text = "";
            foreach (Category cat in topvenue2.Categories)
            {
                topvenue2category.Text += cat.Name;
            }
            topvenue2pic.Image = Globals.loadImageFromURL(topvenue2.Image_url);
            topvenue2pic.Tag = topvenue2.Id;

            newvenue1name.Text = newvenue1.Name;
            newvenue1name.Tag = newvenue1.Id;
            newvenue1location.Text = newvenue1.City + "," + newvenue1.State;
            newvenue1category.Text = "";
            foreach (Category cat in newvenue1.Categories)
            {
                newvenue1category.Text += cat.Name;
            }
            newvenue1pic.Image = Globals.loadImageFromURL(newvenue1.Image_url);
            newvenue1pic.Tag = newvenue1.Id;

            newvenue2name.Text = newvenue2.Name;
            newvenue2name.Tag = newvenue2.Id;
            newvenue2location.Text = newvenue2.City + "," + newvenue2.State;
            newvenue2category.Text = "";
            foreach (Category cat in newvenue2.Categories)
            {
                newvenue2category.Text += cat.Name;
            }
            newvenue2pic.Image = Globals.loadImageFromURL(newvenue2.Image_url);
            newvenue2pic.Tag = newvenue2.Id;

            newvenue3name.Text = newvenue3.Name;
            newvenue3name.Tag = newvenue3.Id;
            newvenue3location.Text = newvenue3.City + "," + newvenue3.State;
            newvenue3category.Text = "";
            foreach (Category cat in newvenue3.Categories)
            {
                newvenue3category.Text += cat.Name;
            }
            newvenue3pic.Image = Globals.loadImageFromURL(newvenue3.Image_url);
            newvenue3pic.Tag = newvenue3.Id;

            newvenue4name.Text = newvenue4.Name;
            newvenue4name.Tag = newvenue4.Id;
            newvenue4location.Text = newvenue4.City + "," + newvenue4.State;
            newvenue4category.Text = "";
            foreach (Category cat in newvenue4.Categories)
            {
                newvenue4category.Text += cat.Name;
            }
            newvenue4pic.Image = Globals.loadImageFromURL(newvenue4.Image_url);
            newvenue4pic.Tag = newvenue4.Id;
        }

        #endregion

        #region Form Events

        private void HomeMDI_Load(object sender, EventArgs e)
        {
            IList<Event> upcomingEvts = Globals.getEventService().getUpcomingEvents(8);
            if (upcomingEvts.Count == 8)
            {
                event1 = upcomingEvts[0];
                event2 = upcomingEvts[1];
                event3 = upcomingEvts[2];
                event4 = upcomingEvts[3];
                event5 = upcomingEvts[4];
                event6 = upcomingEvts[5];
                event7 = upcomingEvts[6];
                event8 = upcomingEvts[7];
            }

            IList<int> topLocations = Globals.getLocationService().getHighestRatedLocations(2);
            if (topLocations.Count == 2)
            {
                topvenue1 = Globals.getLocationService().getById(topLocations[0]);
                topvenue2 = Globals.getLocationService().getById(topLocations[1]);
            }

            IList<Location> newLocations = Globals.getLocationService().getNewestLocations(4);
            if (newLocations.Count == 4)
            {
                newvenue1 = newLocations[0];
                newvenue2 = newLocations[1];
                newvenue3 = newLocations[2];
                newvenue4 = newLocations[3];
            }

            populateHomePage();
        }

        /// <summary>
        /// Displays the event page for the event clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void eventname_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Event load = Globals.getEventService().getById((int)((LinkLabel)sender).Tag);
            MainForm.SelectMDIForm(MainForm.eventFrm);
            MainForm.eventFrm.load_event(load);
        }

        /// <summary>
        /// Displays the venue page for the event picture clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void eventpic_Click(object sender, EventArgs e)
        {
            Location load = Globals.getLocationService().getById((int)((PictureBox)sender).Tag);
            MainForm.venueFrm.load_venue(load);
            MainForm.SelectMDIForm(MainForm.venueFrm);
        }

        /// <summary>
        /// Displays the venue page for the venue clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void venuename_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Location load = Globals.getLocationService().getById((int)((LinkLabel)sender).Tag);
            MainForm.venueFrm.load_venue(load);
            MainForm.SelectMDIForm(MainForm.venueFrm);
        }

        /// <summary>
        /// Displays the venue page for the venue picture clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void venuepic_Click(object sender, EventArgs e)
        {
            Location load = Globals.getLocationService().getById((int)((PictureBox)sender).Tag);
            MainForm.venueFrm.load_venue(load);
            MainForm.SelectMDIForm(MainForm.venueFrm);
        }

        #endregion
        
    }
}